#!/bin/bash
kubectl get node --no-headers=true|awk '{print $2}'|grep NotReady
flag=`echo $?`
echo "flag="$flag
if [[ $flag -eq 0 && `cat /tmp/numzz` -eq 0 ]];then
     echo "node down"
     nodename=`kubectl get node --no-headers=true|grep NotReady|awk '{print $1}'`
     nodeip=`kubectl get node --no-headers=true -owide|grep NotReady|awk '{print $6}'`
     rabbitnode=`kubectl get pod -n public-service -owide|grep rabbit|awk '{print $7}'`
     rabbitmqpod=`kubectl get pod -n public-service -owide|grep rabbit|awk '{print $1}'`
     if [[ $nodename == $rabbitnode ]];then
         kubectl delete pod/$rabbitmqpod --force --grace-period=0 -n  public-service
     fi
     ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${nodename}-${nodeip} Node failure has abnormal"|mail -s "Abnormal\ cluster\ node\ in\ K8S\ development\ and\ perfuat\ environment" zhang.zhe@y3.sg,kkarthick@biznessforce.com"  
     echo 1 >  /tmp/numzz
###############################################################
     #crontab -l > /tmp/temp_crontab
     #sed 's!\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!\#\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!g' /tmp/temp_crontab > /tmp/new_crontab
     #crontab /tmp/new_crontab
     #rm -rf /tmp/{new_crontab,temp_crontab}
##################################################################
elif [[ $flag -eq 1 && `cat /tmp/numzz` -eq 1 ]];then
    echo "node up"
    ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${nodename}-${nodeip} Node failure has been restored"|mail -s "Cluster\ node\ recovery\ in\ K8S\ development\ and\ perfuat\ environment" zhang.zhe@y3.sg,kkarthick@biznessforce.com"
    echo 0 >  /tmp/numzz
###############################################################
     #crontab -l > /tmp/temp_crontab
     #sed 's!\#\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!g' /tmp/temp_crontab > /tmp/new_crontab
     #crontab /tmp/new_crontab
     #rm -rf /tmp/{new_crontab,temp_crontab}
##################################################################
fi
